

/**
 *
 *
 * API
 * ----------------------
 * fromXML(xmlNode)
 * getId()
 * getName()
 * getPolicyName()
 *
 * getActor()
 * getDownLinks()
 * getUpLinks()
 * addDonwLink(link)
 * addUpLink(link)
 * getDownNodes()
 * getUpNodes()
 * 
 * getState()
 * setState(state)
 * 
 * 
 * getX()
 * setX(x)
 * getY()
 * setY(y)
 * addNextLineSize()
 * getNextLineSize()
 * addPreLineSize()
 * getPreLineSize()
 * setSkip(isSkip)
 * isSkip()
 * isDelete()
 * setDelete(isDelete)
 * setVirtual(isVirtual)
 * isVirtual()
 * isSplit()
 * isJoin()
 * isStart()
 * isEnd()
 * isBlank()
 * isHidden()
 * setNextSubSize(subSize)
 * getNextLineSize()
 * setSubSize(subSize)
 * getSubSize()
 */
(function(global){

    'use strict';

    const logger = WorkflowUtil.newLogger("WorkflowApp/BPMAbstractNode");

    let BPMAbstractNode = function () {

        BPMAbstractNode.__super__.constructor.apply(this, arguments);

        this.addAttr("id", "", "i");
        this.addAttr("name", "", "n");
        this.addAttr("type", "", "t");
        this.addAttr("desc", "", "d");


        this.addAttr("x", "0", "x");
        this.addAttr("y", "0", "y");


        ////多级会签自动增加自己，添加节点标识
        this.addAttr("fromType", "", "q");


        //基本属性
        this.downLinks = [];
        this.upLinks = [];

        //基本属性
        this.actor = new global.BPMActor();
        this.policy = new global.BPMSeeyonPolicy();

        // 节点状态
        this.state = WorkflowConstant.nodeState.STATE_NORMAL;


        // 坐标信息
        this.locationInfo = {
            //节点位置信息
            x: 0,
            y: 0.0,
            //有效子连线数量(流程图实际连线数量)
            nextLineSize: 0,
            //有效父连线数量(流程图实际连线数量)
            preLineSize: 0,
            //直接子节点数量
            nextSubSize: 1,
            //子节点+子节点之和的(主要是split节点)
            subSize: 1,

            //split和join如果是单线连接的话画图的时候跳过
            skip: false,

            //是否是删除
            _delete: false,

            //虚拟节点（被删除的节点, 画图属性）
            virtual: false
        }
    };
    //继承
    WorkflowUtil.extend(BPMAbstractNode, global.BPMObject);

    
    BPMAbstractNode.prototype.getState = function(){
        return this.state;
    };
    
    BPMAbstractNode.prototype.setState = function(state){
        this.state = state;
    };
    
    
    BPMAbstractNode.prototype.getBPMObjectType = function(){
      //子节点都需要实现这个方法
    };

    //get函数区域
    BPMAbstractNode.prototype.getId = function(){
      return this.getAttr("id");
    };

    BPMAbstractNode.prototype.getName = function(){
      return this.getAttr("name");
    };

    /**
     * 获取节点权限名称
     */
    BPMAbstractNode.prototype.getPolicyName = function(){
        let policyName = "";
        if(this.policy){
        policyName = this.policy.getName();
      }
      return policyName;
    };

    /**
     *
     * 解析xmlNode
     *
     */
    BPMAbstractNode.prototype.fromXML = function(xmlNode){

        //解析policy属性
        const policyNodes = xmlNode.getElementsByTagName("s");
        if(policyNodes && policyNodes[0]){
            this.policy.fromXML(policyNodes[0]);
        }

      //解析policy属性
        if(this.actor){
            //join, split 没有 actor
            const actors = xmlNode.getElementsByTagName("a");
            if(actors && actors[0]){
                this.actor.fromXML(actors[0]);
            }
        }
    };

    BPMAbstractNode.prototype.toXML = function(pNode, docRoot){

        const nNode = docRoot.createElement("n");
        pNode.appendChild(nNode);

        if(this.actor){
            this.actor.toXML(nNode, docRoot);
        }

        if(this.policy){
            this.policy.toXML(nNode, docRoot);
        }

        this.parseAttr2Node(nNode);
    };

    BPMAbstractNode.prototype.getActor = function(){
      return this.actor;
    };

    BPMAbstractNode.prototype.getPolicy = function(){
      return this.policy;
    };

    BPMAbstractNode.prototype.getDownLinks = function(){
      return this.downLinks;
    };

    BPMAbstractNode.prototype.getUpLinks = function(){
      return this.upLinks;
    };

    BPMAbstractNode.prototype.addDonwLink = function(link){
      this.downLinks.push(link);
    };

    BPMAbstractNode.prototype.addUpLink = function(link){
      this.upLinks.push(link);
    };

    BPMAbstractNode.prototype.getDownNodes = function(){
        const retNodes = [];
        let i = 0, downLinksLength = this.downLinks.length;
        for(; i < downLinksLength; i++){
        retNodes.push(this.downLinks[i].getToNode());
      }
      return retNodes;
    };

    BPMAbstractNode.prototype.getUpNodes = function(){
        const retNodes = [];
        let i = 0, upLinksLength = this.upLinks.length;
        for(; i < upLinksLength; i++){
        retNodes.push(this.upLinks[i].getFromNode());
      }
      return retNodes;
    };

    BPMAbstractNode.prototype.getX = function(){
      return this.locationInfo.x;
    };

    BPMAbstractNode.prototype.setX = function(x){
      this.setAttr("x", x);
      this.locationInfo.x = x;
    };

    BPMAbstractNode.prototype.getY = function(){
      return this.locationInfo.y;
    };

    BPMAbstractNode.prototype.setY = function(y){
      this.setAttr("y", y);
      this.locationInfo.y = y;
    };

    BPMAbstractNode.prototype.addNextLineSize = function(){
      this.locationInfo.nextLineSize++;
    };

    BPMAbstractNode.prototype.getNextLineSize = function(){
      return this.locationInfo.nextLineSize;
    };

    BPMAbstractNode.prototype.addPreLineSize = function(){
      this.locationInfo.preLineSize++;
    };

    BPMAbstractNode.prototype.getPreLineSize = function(){
      return this.locationInfo.preLineSize;
    };

    BPMAbstractNode.prototype.setSkip = function(isSkip){
      this.locationInfo.skip = isSkip;
    };

    BPMAbstractNode.prototype.isSkip = function(){
      return this.locationInfo.skip;
    };

    BPMAbstractNode.prototype.isDelete = function(){
      return this.locationInfo._delete || this.policy.isDelete();
    };

    BPMAbstractNode.prototype.setDelete = function(isDelete){
      this.locationInfo._delete = isDelete;
    };

    
    BPMAbstractNode.prototype.setVirtual = function (isVirtual) {
      this.locationInfo.virtual = isVirtual;
    };

    BPMAbstractNode.prototype.isVirtual = function(){
      return this.locationInfo.virtual;
    };

    BPMAbstractNode.prototype.isHidden = function(){
      return this.isDelete() && !this.isVirtual();
    };

    BPMAbstractNode.prototype.isSplit = function(){
      return this.getBPMObjectType() === WorkflowConstant.bpmObjType.BPMAndRouter
                    && this.getAttr("start") === "true";
    };

    BPMAbstractNode.prototype.isJoin = function(){
      return this.getBPMObjectType() === WorkflowConstant.bpmObjType.BPMAndRouter
                    && this.getAttr("start") !== "true";
    };
    
    BPMAbstractNode.prototype.isStart = function(){
        return this.getId() === "start";
    };
    
    BPMAbstractNode.prototype.isEnd = function(){
        return this.getId() === "end";
    };

    BPMAbstractNode.prototype.isBlank = function(){

      if(this.actor == null){
         return false;
      }

      return "BlankNode" === this.actor.getPartyId();
    };

    BPMAbstractNode.prototype.setNextSubSize = function(subSize){
      this.locationInfo.nextSubSize = subSize;
    };

    BPMAbstractNode.prototype.getNextSubSize = function(){
      return this.locationInfo.nextSubSize;
    };

    BPMAbstractNode.prototype.setSubSize = function(subSize){
      this.locationInfo.subSize = subSize;
    };

    BPMAbstractNode.prototype.getSubSize = function(){
      return this.locationInfo.subSize;
    };

    global.BPMAbstractNode = BPMAbstractNode;

})(WorkflowApp);
